package com.jxud.service.impl;

import com.jxud.mapper.DeptMapper;
import com.jxud.mapper.EmpMapper;
import com.jxud.pojo.Dept;
import com.jxud.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private EmpMapper empMapper;

    /**
     * 查询全部部门
     *
     * @return
     */
    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    /**
     * 根据id 删除部门
     *
     * @param id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Integer id) {
        //先删除部门下的员工
        empMapper.deleteByDeptId(id);
        //员工删除成功后在删除部门
        deptMapper.deleteById(id);

    }

    @Override
    public void add(Dept dept) {
        dept.setCreatedAt(LocalDateTime.now());
        deptMapper.add(dept);
    }
}
